import { IFeidaoAiNodejsComponent } from '@dfeidao/atom-nodejs/interfaces';
import nodejs from '@dfeidao/atom-nodejs/msg/nodejs';
import render from '@dfeidao/atom-nodejs/render/render';
import p001 from './p001';
import p003 from './p003';
import format from '@dfeidao/atom-web/dt/format';

export default async function na001(fd: IFeidaoAiNodejsComponent) {
	// todo
	const res = await nodejs<{ dt1: Array<{ work_bill_no: string, job_content: string, cust_name: string, project_name: string, work_order_status: string, responsible: string, source_type: string, revisit_days: string, cust_sat: string }>; c: number }>(fd.data.actionid, fd.data.sessionid, "order-satisfaction/zj-000002/s001",
		{
			filter: {

				Pn: 1
			}
		});
	for (let i = 0; i < res.dt1.length; i++) {
		if (res.dt1[i].source_type === "1") {
			res.dt1[i].source_type = "总服务台";
		} else if (res.dt1[i].source_type === "2") {
			res.dt1[i].source_type = "服务台";

		} else if (res.dt1[i].source_type === "3") {
			res.dt1[i].source_type = "客户报障";

		} else {
			res.dt1[i].source_type = "内部报障";
		}

	}
	const ret = Array();
	for (let i = 0; i < res.dt1.length; i++) {
		ret[i] = res.dt1[i].work_bill_no;
	}
	const rer = await nodejs<{ dt1: Array<{ work_bill_no: string, revisit_days: number, cust_sat: string }>; dt2: Array<{ cust_sat: string }> }>(fd.data.actionid, fd.data.sessionid, "order-satisfaction/zj-000002/s002", { filter: { work_bill_no: ret } });
	const p = rer.dt1.map((n) => {
		return { ...n, revisit_days: format(n.revisit_days, 2) }
	});
	const q = Array();
	for (let index = 0; index < res.dt1.length; index++) {
		for (let i = 0; i < rer.dt1.length; i++) {
			if (res.dt1[index].work_bill_no === rer.dt1[i].work_bill_no) {
				q[index] = {
					work_bill_no: res.dt1[index].work_bill_no, job_content: res.dt1[index].job_content, source_type: res.dt1[index].source_type,
					cust_name: res.dt1[index].cust_name, project_name: res.dt1[index].project_name, work_order_status: res.dt1[index].work_order_status,
					responsible: res.dt1[index].responsible, revisit_days: p[index].revisit_days,cust_sat: p[index].cust_sat
				};

			}
		}
	}
	for (let i = 0; i < q.length; i++) {
		if (q[i].cust_sat == "1") {
			q[i].cust_sat = "非常满意";
		} else if (q[i].cust_sat == "2") {
			q[i].cust_sat = "满意";

		} else if (q[i].cust_sat == "3") {
			q[i].cust_sat = "一般";

		} else if (q[i].cust_sat == "4") {
			q[i].cust_sat = "不满意";
		} else {
			q[i].cust_sat = "未回访";

		}
	}
		render(fd.data.node, q, p001, 'p001');
		if(q.length>20){
		render(fd.data.node, res.c, p003, 'p003');
		}
	}

